package ru.javadevelop.vegetarian.background;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import ru.javadevelop.vegetarian.common.FileUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * Класс, который умеет скачивать картинки и класть их в локальную папочку. Скачивает только png-картинки
 */
public class ImageGetter {
    private String baseUrlToImagePath;
    private String pathOnFileSystem;
    private Context _context;
    //******************************************************************************************************************
    /**
     * Конструктор.
     * @param baseUrlToImagePath - базовый УРЛ. Например, http://veget.33kita.ru/Content/images/
     */
    public ImageGetter(Context context, String baseUrlToImagePath)
    {
        this.baseUrlToImagePath = baseUrlToImagePath;
        this._context = context;
    }
    //******************************************************************************************************************
    public void  getImage(int id)
    {
        try {
            InputStream imageStream =  openHttpConnection(baseUrlToImagePath+String.valueOf(id)+".png");
            if (imageStream!=null)
            {
                FileUtils.createAppDirOnSD("vegetrecipes");
                FileUtils.saveImageFromStream(_context, imageStream, String.valueOf(id)+".png");
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //******************************************************************************************************************
    private InputStream openHttpConnection(String urlString)
            throws IOException
    {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try{
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        }
        catch (Exception ex)
        {
            throw new IOException("Error connecting");
        }
        return in;
    }
    private Bitmap downloadImage(String URL)
    {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = openHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;
    }


}
